package com.dms.vuemusic.controller;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.dms.vuemusic.pojo.User;
import com.dms.vuemusic.service.impl.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.http.HttpResponse;

@CrossOrigin(origins="*")
@RestController
public class LoginController {
    @Autowired
    private  UserService userService;
    @PostMapping("/login")
    public ResponseEntity<String> login(String username, String password, HttpServletResponse httpResponse) {
//       User user = userService.findUserByName(username);
        User user=userService.findUserByNameAndPassword(username, password);
        System.out.println(user);
        if (user==null) {
            return new  ResponseEntity<>("登陆失败",HttpStatus.FAILED_DEPENDENCY);
        }
        Cookie cookie=new Cookie("username",username);

        cookie.setMaxAge(20);
        httpResponse.addCookie(cookie);
        cookie.setPath("/");


        return new ResponseEntity<>("success", HttpStatus.OK);
    }

}
